//
//  FCJGViewController.m
//  Rong360
//
//  Created by tianliwei on 1/3/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "FCJGViewController.h"
#import "DetailWebViewController.h"
#import "FCJGCell.h"
typedef enum {
    HouseSecondHand,//二手房
    HouseNew//新房
}HouseHandType;

@interface FCJGViewController ()
@property (nonatomic, assign)HouseHandType houseHand;
@property (nonatomic, strong)NSArray *firstMainItems;
@property (nonatomic, strong)NSArray *firstSubItems;
@property (nonatomic, strong)NSArray *secondMainItems;
@property (nonatomic, strong)NSArray *secondSubItems;
@property (nonatomic, strong)NSString *labelTitle;
@property (nonatomic, strong) UISegmentedControl *segmentedControl;

@end

static NSString *CellIdentifier = @"Cell";


@implementation FCJGViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"房产交割";
    self.labelTitle = @"物业交割清单";
    [self initSegmentControl];
    self.houseHand = HouseSecondHand;
    [self.tableView registerNib:[UINib nibWithNibName:@"FCJGCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 44;
    NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"fcjg" ofType:@"plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    
    NSDictionary *first = [dic objectForKey:@"new"];
    self.firstMainItems = [first objectForKey:@"main"];
    self.firstSubItems = [first objectForKey:@"sub"];
    
    NSDictionary *second = [dic objectForKey:@"second"];
    self.secondMainItems = [second objectForKey:@"main"];
    self.secondSubItems = [second objectForKey:@"sub"];

}

- (void)initSegmentControl{
    
    NSArray *segmentedArray = [[NSArray alloc]initWithObjects:@"二手房",@"新房",nil];
    
    //初始化UISegmentedControl
    
    self.segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedArray];
    
    self.segmentedControl.frame = CGRectMake(20.0, 20.0, 280.0, 35.0);
    
    self.segmentedControl.selectedSegmentIndex = 0;//设置默认选择项索引
    
    self.segmentedControl.tintColor = RGBCOLOR(0, 172, 238);
    
    //有基本四种样式
    self.segmentedControl.segmentedControlStyle = UISegmentedControlStylePlain;//设置样式
    
    [self.segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];  //添加委托方法
    [self.view addSubview:self.segmentedControl];
}


//具体委托方法实例

-(void)segmentAction:(UISegmentedControl *)Seg{
    NSInteger Index = Seg.selectedSegmentIndex;
    switch (Index) {
        case 0:
            self.houseHand = HouseSecondHand;
            self.labelTitle = @"物业交割清单";
            break;
        case 1:
            self.houseHand = HouseNew;
            self.labelTitle = @"新房验房清单";
            break;
        default:
            break;
    }
    [self.tableView reloadData];
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *v = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 30)];
    v.backgroundColor = RGBCOLOR(245, 245, 245);
    UILabel *label = [UILabel labelWithFrame:CGRectMake(20, 0, UI_SCREEN_WIDTH-20, 30) title:self.labelTitle fontColor:[UIColor lightGrayColor] fontSize:15];
    [v addSubview:label];
    return v;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FCJGCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (self.houseHand == HouseSecondHand) {
        cell.mainLabel.text = [self.secondMainItems objectAtIndex:indexPath.row];
    }else if (self.houseHand == HouseNew){
        cell.mainLabel.text = [self.firstMainItems objectAtIndex:indexPath.row];
    }
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.houseHand == HouseSecondHand) {
        return self.secondMainItems.count;
    }else{
        return self.firstMainItems.count;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.hidesBottomBarWhenPushed = YES;
    NSString *contentStr = nil;
    if (self.houseHand == HouseSecondHand) {
        contentStr = self.secondSubItems[indexPath.row];
    }else if (self.houseHand == HouseNew){
        contentStr = self.firstSubItems[indexPath.row];
    }
    DetailWebViewController *detailVC = [[DetailWebViewController alloc]initWithContent:contentStr];
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
